/*
package com.ruoyi.project.tool.swagger.UserController;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.druid.wall.violation.ErrorCode;
import com.alibaba.fastjson.JSONObject;
import com.icbc.api.utils.IcbcEncrypt;
import com.icbc.api.utils.IcbcSignature;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.sign.AesUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Ide;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.OperatorType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysBank;
import com.ruoyi.project.system.domain.SysCompany;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysReceive;
import com.ruoyi.project.system.domain.vo.UserInfo;
import com.ruoyi.project.system.domain.vo.WxLoginInfo;
import com.ruoyi.project.system.service.ISysBankService;
import com.ruoyi.project.system.service.ISysCompanyService;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysReceiveService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
//@RequestMapping("/mobileUser")
public class UserController extends BaseController {
    @Autowired
    private SendUtils sendUtils;
    @Autowired
    private RuoYiConfig ruoyiConfig;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysBankService sysBankService;

    @Autowired
    private ISysReceiveService sysReceiveService;

    @Autowired
    private ISysCompanyService sysCompanyService;

    private Lock lock = new ReentrantLock(true);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static String encrypt_type = "AES";
    private static String sign_type = "RSA2";
    private static String charset = "UTF-8";


    @ApiOperation("获取用户详细")
    @GetMapping("getInfo")
    public AjaxResult getUser() {
        return AjaxResult.success(SecurityUtils.getLoginUser().getUser());
    }


    public String gettoken(String code) throws UnsupportedEncodingException {
        String openid = "";
        String strAppId = wxMaService.getWxMaConfig().getAppid();//就是appid
        String strSecret = wxMaService.getWxMaConfig().getSecret();//就是secret
        String strUrl = " https://api.weixin.qq.com/sns/oauth2/access_token";
        strUrl += "?appid=" + strAppId;
        strUrl += "&secret=" + strSecret;
        strUrl += "&code=" + code; //微信反馈的code
        strUrl += "&grant_type=authorization_code";
        byte[] res = null;

        org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet();
        try {
            URL url = new URL(strUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            httpget = new HttpGet(uri);
            HttpResponse response = null;
            response = httpclient.execute(httpget);
            res = IOUtils.toByteArray(response.getEntity().getContent());
        } catch (Exception e) {
            throw new BaseException("微信获取错误!");

        } finally {
            if (httpget != null) {
                httpget.abort();
            }
            httpclient.getConnectionManager().shutdown();
        }
        JSONObject jsonObject = JSONObject.parseObject(new String(res, "utf-8"));
        System.out.println(jsonObject.toJSONString());
        if (!jsonObject.containsKey("openid")) {
            throw new CustomException("微信授权报错", 405);
        }
        openid = jsonObject.getString("openid");
        return openid;
    }

    */
/**
     * 微信授权登录
     *//*

    @Ide
    @SignatureValidation
    @PostMapping("authorizedLogin")
    public AjaxResult insertMobileUser(@RequestBody WxLoginInfo wxLoginInfo) throws Exception {
        Map map = new HashMap();
        String code = wxLoginInfo.getCode();
        UserInfo userInfo = wxLoginInfo.getUserInfo();
        if (StringUtils.isEmpty(code)) {
            return AjaxResult.error("参数不正确");
        }
        return AjaxResult.success("获取成功", gettoken(code));
    }


    public AjaxResult getOpenId(HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getHeader("param")) || StringUtils.isEmpty(request.getHeader("sign"))) {
            return AjaxResult.error("参数不正确");
        }
        boolean flag;
        try {
            flag = IcbcSignature.verify(request.getHeader("param"), sign_type, ruoyiConfig.getIcbcPublicKey(), charset, request.getHeader("sign"));
        } catch (Exception e) {
            return AjaxResult.error("获取用户信息错误");
        }
        if (!flag) {
            return AjaxResult.error("获取用户信息错误");
        }
        String decrypt;
        try {
            decrypt = IcbcEncrypt.decryptContent(request.getHeader("param"), encrypt_type, ruoyiConfig.getAESKey(), charset);
        } catch (Exception e) {
            return AjaxResult.error("获取用户信息错误");
        }
        if (StringUtils.isNotEmpty(decrypt)) {
            JSONObject obj = JSONObject.parseObject(decrypt);
            long currentTime = System.currentTimeMillis();
            long timestamp = obj.getLong("timestamp");
            */
/*if (currentTime - timestamp >= 5 * 60 * 1000L){
                return AjaxResult.error("时间戳超时!");
            }*//*

            return AjaxResult.success("操作成功", obj.getString("cust_id"));
        }
        return AjaxResult.error("系统错误!");
    }


    @PostMapping(value = "/sendCode")
    @SignatureValidation
    public AjaxResult sendCode(@RequestBody Map map) {
        return sendUtils.send(map);
    }


    @PostMapping(value = "/codeLogin")
    @Log(title = "动态验证码登录", operatorType = OperatorType.MOBILE)
    @SignatureValidation
    @ApiOperation("动态验证码登录")
    public AjaxResult codeLogin(@RequestBody Map map) {
        if (null == map.get("phone")) {
            return AjaxResult.error("手机号为空");
        }
        if (null == map.get("companyId")) {
            return AjaxResult.error("活动ID为空");
        }
        if (null == map.get("code")) {
            return AjaxResult.error("短信验证码为空");
        }
        if (null == map.get("key")) {
            return AjaxResult.error("验证码值为空");
        }
        SysCompany company = sysCompanyService.selectSysCompanyById(Long.valueOf(map.get("companyId") + ""));
        if (null == company) {
            return AjaxResult.error("未查询到该活动!");
        }
        if (null != company.getFinishTime()) {
            Date date = new Date();
            if (company.getFinishTime().getTime() < date.getTime()) {
                return AjaxResult.error("活动已结束!");
            }
        }
        String verifyKey = Constants.PHONE_CODE_KEY + map.get("phone") + map.get("key");
        String captcha = redisCache.getCacheObject(verifyKey);
        if (captcha == null) {
            return AjaxResult.error("验证码失效");
        }
        if (!(map.get("code") + "").equalsIgnoreCase(captcha)) {
            return AjaxResult.error("验证码错误");
        }
        SysBank sysBank = sysBankService.selectBankByPhoneAndId(map.get("phone") + "", Long.valueOf(map.get("companyId") + ""));
        if (null == sysBank) {
            return AjaxResult.error("未在白名单!");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setSysBank(sysBank);
        loginUser.setUserName(sysBank.getUserName());
        loginUser.setLoginTime(System.currentTimeMillis());
        Object token = tokenService.createToken(loginUser);
        redisCache.deleteObject(verifyKey);
        return AjaxResult.success(token);
    }

    @PostMapping(value = "/getNotMessage")
    @SignatureValidation
    @Log(title = "领取", operatorType = OperatorType.MOBILE)
    public AjaxResult getNotMessage(@RequestBody SysReceive sysReceive) {
        AjaxResult result = AjaxResult.error();
        try {
            lock.lock();
            result = sysReceiveService.valid(sysReceive, true);
            if (Integer.valueOf(result.get("code") + "") != HttpStatus.SUCCESS) {
                return result;
            }
            result = sysReceiveService.insertSysReceiveData(sysReceive);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("系统错误!");
        } finally {
            lock.unlock();
        }
        return result;
    }


    @PostMapping(value = "/getActivityMoney")
    @SignatureValidation
    @Ide
    @Log(title = "领取", operatorType = OperatorType.MOBILE)
    public AjaxResult getActivityMoney(@RequestBody SysReceive sysReceive) {
        AjaxResult result = AjaxResult.error();
        SysCompany sysCompany = sysCompanyService.selectSysCompanyById(sysReceive.getCompanyId());
        try {
            lock.lock();
            if(null==sysCompany){
                return AjaxResult.error("活动不存在!");
            }
            if (StringUtils.isEmpty(sysReceive.getCode())) {
                return AjaxResult.error("验证码为空!");
            }
            if (StringUtils.isEmpty(sysReceive.getKey())) {
                return AjaxResult.error("没有发送验证码!");
            }
            if (StringUtils.isEmpty(sysReceive.getSecret())) {
                return AjaxResult.error("请输入密语!");
            }
            String verifyKey = Constants.CAPTCHA_CODE_KEY + sysReceive.getKey();
            String captcha = redisCache.getCacheObject(verifyKey);
            if (captcha == null) {
                return AjaxResult.error(HttpStatus.EXPIRE,"验证码失效");
            }
            if (!(sysReceive.getCode() + "").equalsIgnoreCase(captcha)) {
                return AjaxResult.error("验证码错误");
            }
            result = sysReceiveService.valid(sysReceive, true);
            if (Integer.valueOf(result.get("code") + "") != HttpStatus.SUCCESS) {
                return result;
            }
            result = sysReceiveService.insertSysReceiveDataDisable(sysReceive);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(e.getMessage());
        } finally {
            lock.unlock();
        }
        return result;
    }

    @PostMapping(value = "/getActivityMoney/disable")
    @SignatureValidation
    @Log(title = "领取", operatorType = OperatorType.MOBILE)
    public AjaxResult disable(@RequestBody SysReceive sysReceive) {
        AjaxResult result = AjaxResult.error();
        try {
            lock.lock();

            if (StringUtils.isEmpty(sysReceive.getCode())) {
                return AjaxResult.error("短信验证码为空");
            }
            if (StringUtils.isEmpty(sysReceive.getKey())) {
                return AjaxResult.error("没有发送验证码");
            }
            String verifyKey = Constants.PHONE_CODE_KEY + sysReceive.getPhone() + sysReceive.getKey();
            String captcha = redisCache.getCacheObject(verifyKey);
            if (captcha == null) {
                return AjaxResult.error("验证码失效");
            }
            if (!(sysReceive.getCode() + "").equalsIgnoreCase(captcha)) {
                return AjaxResult.error("验证码错误");
            }
            result = sysReceiveService.validDisable(sysReceive, true);
            if (Integer.valueOf(result.get("code") + "") != HttpStatus.SUCCESS) {
                return result;
            }
            result = sysReceiveService.insertSysReceiveDataDisable(sysReceive);
            redisCache.deleteObject(verifyKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("系统错误!");
        } finally {
            lock.unlock();
        }
        return result;
    }


    @PostMapping(value = "/callback")
    @Log(title = "核销回调", operatorType = OperatorType.MANAGE)
    public Map callback(@RequestBody JSONObject obj) {
        System.out.println(obj.toJSONString());
        Map map = new HashMap();
        if (null != obj) {
            AesUtil aesUtil = new AesUtil("qwertyuiopasdfghjklzxc1234567890".getBytes());
            JSONObject data = obj.getJSONObject("resource");
            try {
                String str = aesUtil.decryptToString(data.getString("associated_data").getBytes(), data.getString("nonce").getBytes(), data.getString("ciphertext"));
                JSONObject receive = JSONObject.parseObject(str);
                rabbitTemplate.convertAndSend("fanoutExchange", "", receive);
                map.put("code", "SUCCESS");
                map.put("message", "核销成功");
                return map;
            } catch (Exception e) {
                e.printStackTrace();
                map.put("message", "解密错误");
                return map;
            }
        }
        return map;
    }

    */
/**
     * 查询领取列表
     *//*

    @ApiOperation("查询领取列表" )
    @GetMapping("/receive/list" )
    public TableDataInfo list(SysReceive sysReceive) {
        if(null!=sysReceive.getCompanyId()){
            SysCompany company=sysCompanyService.selectSysCompanyById(sysReceive.getCompanyId());
            if(null!=company){
                sysReceive.setNickName(company.getName());
            }
        }
        startPage();
        List<SysReceive> list = sysReceiveService.selectSysReceiveList(sysReceive);
        return getDataTable(list);
    }


    @PostMapping("/getSign")
    public AjaxResult luck(@RequestBody Map map) throws WxErrorException {
        if (null == map) {
            return AjaxResult.error("参数错误!");
        }
        if(null==map.get("url")){
            return AjaxResult.error("地址错误!");
        }
        WxJsapiSignature wxJsapiSignature = wxMaService.getJsapiService().createJsapiSignature(map.get("url")+"");
        return AjaxResult.success(wxJsapiSignature);
    }

}
*/
